<?php

/* 积分数据模型 */
class IntegralModel extends BaseModel
{
    var $table  = 'integral';
    var $prikey = 'user_id';
    var $alias  = 'ig';
    var $_name  = 'integral';

	function update_integral($data = array())
	{
		extract($data);
		
		if(!in_array($type,array('return_integral','buying_has_integral','selling_has_integral')) && !$this->_get_sys_setting('integral_enabled'))
		{
			return;
		}
		
		if(empty($amount) || empty($user_id))
		{
			return;
		}
		
		$integral = parent::get($user_id);
		$result = array(
			'user_id'=> $user_id,
			'amount' => $flow == 'minus' ? -$amount: $amount
		);
		if(empty($integral))
		{
			parent::add($result);
		}
		else
		{
			if($result['amount']+$integral['amount'] < 0)
			{
				echo "余额不足";
				exit;
			}
			parent::edit($user_id,array('amount'=>$result['amount']+$integral['amount']));
		}
		
		$data['balance'] = $result['amount']+$integral['amount'];// 积分余额	
		$integral_log_mod = &m('integral_log');
		$integral_log_mod->add_log($data);
	}
	
	// 订单取消后，归还买家之前被预扣积分
	function return_integral($order_info)
	{
		$order_integral_mod = &m('order_integral');
		$order_integral = $order_integral_mod->get($order_info['order_id']);
		
		$integral_log_mod = &m('integral_log');
		$log = $integral_log_mod->get('order_id='.$order_info['order_id']);
		if($order_integral)
		{
			$data=array(
				'user_id' => $order_info['buyer_id'],
				'type'    => 'return_integral',
				'order_id'=> $order_info['order_id'],
				'order_sn'=> $order_info['order_sn'],
				'amount'  => $order_integral['frozen_integral'],
			 	'flag'    => Lang::get('return_integral_for_cancel_order')	
			);
			
			$this->update_integral($data);
			$integral_log_mod->edit($log['log_id'],array('state'=>'cancel'));
			$order_integral_mod->drop($order_info['order_id']);
		}
	}
	
	//订单完成后分发积分。该操作可以不受是否开启积分的影响
	function distribute_integral($order_info)
	{
		$order_integral_mod = &m('order_integral');
		$frozen_integral = $order_integral_mod->get($order_info['order_id']);
		
		$store_mod = &m('store');
		$store = $store_mod->get(array(
			'conditions' => 'store_id='.$order_info['seller_id'],
			'fields'     => 'sgrade'
		));

		//订单完成给买家赠送积分
		$buy_has_integral = round($order_info['goods_amount']*$this->_get_sys_setting(array('buying_integral',$store['sgrade'])),2);
		$this->update_integral(array(
			'user_id' => $order_info['buyer_id'],
			'type'    => 'buying_has_integral',
			'amount'  => $buy_has_integral,
			'order_id'=> $order_info['order_id'],
			'order_sn'=> $order_info['order_sn']
		));
		
		//把冻结的积分分发给商家
		$this->update_integral(array(
			'user_id' => $order_info['seller_id'],
			'type'    => 'selling_has_integral',
			'amount'  => $frozen_integral['frozen_integral'],
			'order_id'=> $order_info['order_id'],
			'order_sn'=> $order_info['order_sn']
		));	
		
		//把冻结的记录状态改为完成
		$integral_log_mod = &m('integral_log');
		$log = $integral_log_mod->get('order_id='.$order_info['order_id']);
		$integral_log_mod->edit($log['log_id'],array('state'=>'finished'));
		
		$order_integral_mod->drop($order_info['order_id']);
	}
	
	
	//积分变动的状态，完成，取消，冻结
	
	function status($string)
	{
		$status = array(
			'finished' => 'integral_finished',
			'frozen'   => 'integral_frozen',
			'cancel'   => 'integral_cancel'
		);
		
		return Lang::get($status[$string]);
	}
	
	/*
	// 获取系统的积分配置数据，参数为字符串或者数组
	*/
	function _get_sys_setting($param)
	{
		if(!$param)
		{
			return 0;
		}
		$setting = Conf::get('integral_manage');
		if(empty($setting))
		{
			return 0;
		}
		else
		{
			if(is_array($param))
			{
				list($val1,$val2) = $param;//只要前两个值
				return $setting[$val1][$val2];
			}
			else
			{
				return($setting[$param]);
			}
		}
	}
	
}

?>